﻿using Prism.Navigation.TabbedPages;
using TangReadyV1.Models;
using TangReadyV1.ViewModels;
using SQLite;
using Syncfusion.Data.Extensions;
using Syncfusion.XForms.Buttons;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using Xamarin.Forms;

namespace TangReadyV1.Views
{
    public partial class PageResultView : ContentPage
    {
       // ObservableCollection tObjectStruct = new ObservableCollection(tList);
        ObservableCollection<SaleTableFinished> obsSales = new ObservableCollection<SaleTableFinished>();
     //   ObservableCollection<SaleTable> obsSales = new ObservableCollection<SaleTable>();
        public PageResultView()
        {
            InitializeComponent();
            ((PageResultViewViewModel)BindingContext).IsActiveChanged += PageResultView_IsActiveChanged;

           //  obsSales = new ObservableCollection<SaleTable>();
           // List<SaleTable> one = await App.MiTotalDatabase.GetItemsAsync();
          //  one.ForEach
            this.listToUpload.ItemsSource = obsSales;
            string str = DateTime.Now.ToShortDateString();

            DateTime time1 = Convert.ToDateTime(str + " 0:00:00");
            DateTime time2 = Convert.ToDateTime(str + " 23:59:59");
            startDate.Date = time1;
            endDate.Date = time2;
        }

        private void PageResultView_IsActiveChanged(object sender, System.EventArgs e)
        {
            if (((PageResultViewViewModel)BindingContext).IsActive)
            {
                this.Title = ((PageResultViewViewModel)BindingContext).Title;
           
            }
        }      

        private async void btnSave_Clicked(object sender, System.EventArgs e)
        {
          

            obsSales.Clear();         

           //  var start = startDate.Date;

           // var end = endDate.Date;

            DateTime start = Convert.ToDateTime( startDate.Date.ToShortDateString()+ " 0:00:00");
            DateTime  end = Convert.ToDateTime(endDate.Date.ToShortDateString() + " 23:59:59");

            var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "TotalSalesSQLite.db3");
            using (SQLiteConnection con = new SQLiteConnection(path))
            {

                List<SaleTableFinished> by2 = con.Table<SaleTableFinished>().Where(t =>t.JYSJ2 >= start && t.JYSJ2 <= end).OrderBy(n => n.JYBH).ToList();

                by2.ForEach(t => obsSales.Add(t));
            }


            //更新统计
            this.saleSum.Text = "销售数据查询(交易数 " + obsSales.Count + "  交易额 " + obsSales.Sum<SaleTableFinished>(b =>(short)b.XSJE) + ")";

        }

        private async void btnDelete_Clicked(object sender, EventArgs e)
        {
            await((PageResultViewViewModel)BindingContext)._navigationService.SelectTabAsync("PageUpload");  
        }


        //求本周一的日期 
        static DateTime GetMondayInWeek(DateTime dt)
        {
            int week = (int)dt.DayOfWeek;

            if (week == 0)
            {
                week = 7;
            }

            if (week == 1)
            {
                return dt;
            }

            else
            {
                DateTime result = dt.AddDays(-(week - 1));
                return result;
            }
        }

            private void SfRadioButton_StateChanged(object sender, Syncfusion.XForms.Buttons.StateChangedEventArgs e)
        {
            if (obsSales!=null)
            {
             //   obsSales.Clear();
            }
            var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "TotalSalesSQLite.db3");
            using (SQLiteConnection con = new SQLiteConnection(path))
            {

                if (e.IsChecked.HasValue && e.IsChecked.Value && (sender as SfRadioButton).Text == "今天")
                {                
                    string str = DateTime.Now.ToShortDateString();
                    DateTime time1 = Convert.ToDateTime(str + " 0:00:00");
                    DateTime time2 = Convert.ToDateTime(str + " 23:59:59");

                    startDate.Date = time1;
                    endDate.Date = time2;                  
                }
                else if (e.IsChecked.HasValue && e.IsChecked.Value && (sender as SfRadioButton).Text == "昨天")
                {               
                    string str = DateTime.Now.AddDays(-1).ToShortDateString();
                    DateTime time1 = Convert.ToDateTime(str + " 0:00:00");
                    DateTime time2 = Convert.ToDateTime(str + " 23:59:59");
                    startDate.Date = time1;
                    endDate.Date = time2;
                  
                }
                else if (e.IsChecked.HasValue && e.IsChecked.Value && (sender as SfRadioButton).Text == "本周")
                { 
                    DateTime startWeek=  GetMondayInWeek(DateTime.Now);
                    DateTime endWeek = startWeek.AddDays(6);  //本周日
                    startDate.Date = startWeek;
                    endDate.Date = endWeek;                   
                }
                else if (e.IsChecked.HasValue && e.IsChecked.Value && (sender as SfRadioButton).Text == "上周")
                {
                    DateTime startWeek = GetMondayInWeek(DateTime.Now).AddDays(-7); //上周一
                    DateTime endWeek = startWeek.AddDays(6);  //上周日
                    startDate.Date = startWeek;
                    endDate.Date = endWeek;                  
                }
                else if (e.IsChecked.HasValue && e.IsChecked.Value && (sender as SfRadioButton).Text == "本月")
                {
                
                    DateTime dt = DateTime.Now;  //当前时间  
                    DateTime startMonth = dt.AddDays(1 - dt.Day);  //本月月初  
                    DateTime endMonth = dt;  //本月今日 
                    startDate.Date = startMonth;
                    endDate.Date = endMonth;
                   
                }
                else if (e.IsChecked.HasValue && e.IsChecked.Value && (sender as SfRadioButton).Text == "上月")
                {
                  
                    DateTime dt = DateTime.Now.AddMonths(-1);  //上个月当前时间  
                    DateTime startMonth = dt.AddDays(1 - dt.Day);  //上月月初  
                    DateTime endMonth = startMonth.AddMonths(1).AddDays(-1);  //上月月末 

                    startDate.Date = startMonth;
                    endDate.Date = endMonth;                  
                }
                else if (e.IsChecked.HasValue && e.IsChecked.Value && (sender as SfRadioButton).Text == "今年")
                {
                
                    DateTime dt = DateTime.Now;  //当前时间  
                    DateTime startYear = new DateTime(dt.Year, 1, 1);  //本年年初  
                    DateTime endYear = dt;  //今日
                    startDate.Date = startYear;
                    endDate.Date = endYear;                    
                }
                else if (e.IsChecked.HasValue && e.IsChecked.Value && (sender as SfRadioButton).Text == "上年")
                {
                    DateTime dt = DateTime.Now.AddYears(-1);  //当前时间  
                    DateTime startYear = new DateTime(dt.Year, 1, 1);  //上年年初  
                    DateTime endYear = new DateTime(dt.Year, 12, 31);  //上年年末 
                    startDate.Date = startYear;
                    endDate.Date = endYear;                  
                }
            }
        }
    }
}
